package org.mydbatest.util;

import oracle.jdbc.pool.OracleConnectionPoolDataSource;
import oracle.jdbc.pool.OracleDataSource;

import javax.sql.PooledConnection;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

public abstract class AbstractOracleConnectionProvider {
    /**
     * Close a connection.
     *
     * @param c the connection
     */
    public static void close(final Connection c) {
        if (c != null) {
            try {
                c.close();
            }
            catch (SQLException sqle) {
                sqle.printStackTrace();
            }
        }
    }

    /**
     * Close a pooled connection.
     *
     * @param pc the connection
     */
    public static void close(final PooledConnection pc) {
        if (pc != null) {
            try {
                pc.close();
            }
            catch (SQLException sqle) {
                sqle.printStackTrace();
            }
        }
    }

    /**
     * Close a statement.
     *
     * @param s the statement
     */
    public static void close(final Statement s) {
        if (s != null) {
            try {
                s.close();
            }
            catch (SQLException sqle) {
                sqle.printStackTrace();
            }
        }
    }

    /**
     * Close a result set.
     *
     * @param r the result set
     */
    public static void close(final ResultSet r) {
        if (r != null) {
            try {
                r.close();
            }
            catch (SQLException sqle) {
                sqle.printStackTrace();
            }
        }
    }

    /**
     * Close a datasource.
     *
     * @param ds the datasource
     */
    public static void close(final OracleConnectionPoolDataSource ds) {
        if (ds != null) {
            try {
                ds.close();
            }
            catch (SQLException sqle) {
                sqle.printStackTrace();
            }
        }
    }

    /**
     * Close a datasource.
     *
     * @param ds the datasource
     */
    public static void close(final OracleDataSource ds) {
        if (ds != null) {
            try {
                ds.close();
            }
            catch (SQLException ignored) {
            }
        }
    }
}
